<template>
	<view class="my-page">
		<u-navbar leftIconColor="#ffffff" :autoBack="true" :placeholder="true" bgColor="rgba(255,255,255,0.0)">
			<view slot="center" class="nav-title">
				个人信息
			</view>
		</u-navbar>
		<view class="box">
			<view class="head" @click="changeAvatar">
				<image :src="info.u_img" mode="aspectFill"></image>
				<view class="avatar-edit">点击修改</view>
			</view>
			<view class="item-box">
				<view class="item">
					<view class="item-text">
						昵称
					</view>
					<view class="item-right" @click="showNicknameEdit">
						<view class="right-text">
							{{username || '未设置'}}
						</view>
						<u-icon name="arrow-right" color="#999999" size="16"></u-icon>
					</view>
				</view>
			</view>
		</view>

		<!-- 修改昵称弹窗 -->
		<u-modal :show="showNickModal" title="修改昵称" :showCancelButton="true" cancelText="取消" confirmText="确定"
			:asyncClose="true" @confirm="saveNickname" @cancel="showNickModal = false" :closeOnClickOverlay="false">
			<view class="modal">
				<u-input v-model="username" placeholder="请输入新昵称" maxlength="20" border="bottom" clearable></u-input>
				<view class="input-tip">最多20个字符</view>
			</view>
		</u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				info: {},
				showNickModal: false,
				username: '',
				avatarTempPath: '' // 临时存储选择的头像路径
			}
		},
		onLoad() {
			this.postuserinfo()
		},
		methods: {
			postuserinfo() {
				this.$Request({
					method: 'POST',
					url: '/api/personal/myIndex',
					data: {},
					ismsg: false,
				}).then(res => {
					if (res.code == 1) {
						this.info = res.data.user
						this.username = this.info.username || ''
					}
				})
			},

			// 修改头像
			changeAvatar() {
				uni.chooseImage({
					count: 1,
					sourceType: ['album', 'camera'],
					crop: {
						quality: 60, // 高质量保存(90%)
						resize: false // 不强制调整到精确像素
					},
					success: (res) => {
						this.avatarTempPath = res.tempFilePaths[0]
						this.uploadAvatar()
					}
				})
			},

			// 上传头像
			uploadAvatar() {
				let app = this
				uni.showLoading({
					title: '上传中...'
				})
				// 这里需要替换为你的上传接口
				uni.uploadFile({
					url: 'https://api.eachidc.com/api/upload/upload',
					filePath: app.avatarTempPath,
					name: 'file',
					header: {
						'tokenSJZX': uni.getStorageSync("token")
					},
					success: (uploadRes) => {
						const res = JSON.parse(uploadRes.data)
						if (res.code == 1) {
							app.$Request({
								method: 'POST',
								url: '/api/personal/changevoucher',
								data: {
									receive_qrcode: res.url
								},
								ismsg: true,
							}).then(res => {
								if (res.code == 1) {
									app.postuserinfo()
									uni.$u.toast('修改成功')
								}
							})
						} else {
							uni.$u.toast(res.msg || '上传失败')
						}
					},
					fail: () => {
						uni.$u.toast('上传失败')
					},
					complete: () => {
						uni.hideLoading()
					}
				})
			},

			// 显示昵称修改弹窗
			showNicknameEdit() {
				this.username = this.info.username || ''
				this.showNickModal = true
			},

			// 保存昵称
			saveNickname() {
				if (!this.username.trim()) {
					uni.$u.toast('昵称不能为空')
					return
				}

				this.$Request({
					method: 'POST',
					url: '/api/personal/changename',
					data: {
						name: this.username
					},
					ismsg: true,
				}).then(res => {
					if (res.code == 1) {
						this.postuserinfo()
						this.showNickModal = false
						uni.$u.toast('修改成功')
					}
				})
			}
		}
	}
</script>
<style>
	page {
		background: #000000;
	}
</style>
<style lang="scss" scoped>
	.my-page {
		padding: 30rpx;

		.nav-title {
			color: #ffffff;
		}

		.box {
			border-radius: 20rpx;
			padding-top: 60rpx;

			.head {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				margin-bottom: 40rpx;

				image {
					width: 150rpx;
					height: 150rpx;
					border-radius: 50%;
					border: 2rpx solid #f5f5f5;
				}

				.avatar-edit {
					margin-top: 20rpx;
					color: #ffffff;
					border: 1px solid #1A1A1B;
					font-size: 24rpx;
					background: #1A1A1BCC;
					border-radius: 20px;
					padding: 10rpx 20rpx;
				}
			}

			.item-box {
				background: #1A1A1B;
				backdrop-filter: blur(40.099998474121094px);
				border-radius: 8px;
				padding: 16px;

				.item {
					display: flex;
					justify-content: space-between;
					align-items: center;
					.item-text {
						font-size: 30rpx;
						color: #FFFFFF;
					}

					.item-right {
						display: flex;
						align-items: center;

						.right-text {
							color: #999;
							font-size: 28rpx;
							margin-right: 10rpx;
						}
					}
				}
			}
		}

		.modal {
			width: 100%;
			padding: 20rpx 30rpx;

			.u--input {
				padding: 20rpx 0;
			}

			.input-tip {
				font-size: 24rpx;
				color: #999;
				text-align: right;
				margin-top: 10rpx;
			}
		}
	}
</style>